import fsp from 'fs/promises'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'

describe('types-gen-nounuselocal', () => {
  const { next, skipped } = nextTestSetup({
    files: __dirname,
    skipDeployment: true,
  })

  if (skipped) {
    return
  }

  it('should build successfully', async () => {
    const $ = await next.render$('/')
    expect($('p').text()).toBe('hello world')
  })

  it('should generate route types successfully', async () => {
    const routeTypes = await fsp.readFile(
      path.join(next.testDir, '.next', 'types', 'routes.d.ts'),
      'utf-8'
    )

    expect(routeTypes).not.toContain('AppRouteHandlerRoutes')
    expect(routeTypes).not.toContain('PagesPageConfig')
    expect(routeTypes).not.toContain('ApiRouteConfig')

    expect(routeTypes).toMatchInlineSnapshot(`
     "// This file is generated automatically by Next.js
     // Do not edit this file manually

     type AppRoutes = "/"
     type PageRoutes = never
     type LayoutRoutes = "/"
     type RedirectRoutes = never
     type RewriteRoutes = never
     type Routes = AppRoutes | PageRoutes | LayoutRoutes | RedirectRoutes | RewriteRoutes


     interface ParamMap {
       "/": {}
     }


     export type ParamsOf<Route extends Routes> = ParamMap[Route]

     interface LayoutSlotMap {
       "/": never
     }


     export type { AppRoutes, PageRoutes, LayoutRoutes, RedirectRoutes, RewriteRoutes, ParamMap }

     declare global {
       /**
        * Props for Next.js App Router page components
        * @example
        * \`\`\`tsx
        * export default function Page(props: PageProps<'/blog/[slug]'>) {
        *   const { slug } = await props.params
        *   return <div>Blog post: {slug}</div>
        * }
        * \`\`\`
        */
       interface PageProps<AppRoute extends AppRoutes> {
         params: Promise<ParamMap[AppRoute]>
         searchParams: Promise<Record<string, string | string[] | undefined>>
       }

       /**
        * Props for Next.js App Router layout components
        * @example
        * \`\`\`tsx
        * export default function Layout(props: LayoutProps<'/dashboard'>) {
        *   return <div>{props.children}</div>
        * }
        * \`\`\`
        */
       type LayoutProps<LayoutRoute extends LayoutRoutes> = {
         params: Promise<ParamMap[LayoutRoute]>
         children: React.ReactNode
       } & {
         [K in LayoutSlotMap[LayoutRoute]]: React.ReactNode
       }
     }
     "
    `)
  })
})
